import os
import sys
import msvcrt
import IOHandle
import CountLine
import CountCharacter
import CountWord
import json
class Main:
	def __init__(self):
		pass

	@staticmethod
	def Input(fileName):
		if fileName.strip():
			return IOHandle.IoHandler.Read(fileName)
	@staticmethod		
	def Output(text,fileName):
		if fileName.strip():
			IOHandle.IoHandler.Write(text,fileName)
	@staticmethod
	def CountLine(lines):
		return CountLine.CountLine.Count(lines)

	@staticmethod
	def CountCharacter(lines):
		return CountCharacter.CountCharacter.Count(lines)

	@staticmethod
	def CountWord(lines):
		return CountWord.CountWord.Count(lines)

	@staticmethod		
	def WordCount(fileName):
		lines = Main.Input(fileName)
		wordAnalysis = Main.CountWord(lines)
		output = ""

		charactersCount = Main.CountCharacter(lines)
		wordCount = wordAnalysis["count"]
		lineCount = Main.CountLine(lines)

		output = output + "characters:" + str(charactersCount) +'\n'
		output = output + "word:" + str(wordCount) +'\n'
		output = output + "lines:" + str(lineCount) +'\n'

		_dict = wordAnalysis["dict"]
		_keys = wordAnalysis["keys"]
		num = min(10,len(_keys))
		outputWordAnalysis = {}
		for i in range(num):
			output = output + "<" +_keys[i]+">" +":" + str(_dict[_keys[i]])+'\n'
			outputWordAnalysis[_keys[i]] = _dict[_keys[i]]

		outJson = {}
		outJson["charactersCount"] = charactersCount
		outJson["wordCount"] = wordCount
		outJson["lineCount"] = lineCount
		outJson["words"] = outputWordAnalysis
		Main.Output(json.dumps(outJson),"resultJson.txt")

		return output



output = Main.WordCount("input.txt")
Main.Output(output,"result.txt")
